﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class CallingIVRDataAccess : BaseDataAccess<Business.Entities.CallingIVR>
    {
        public override GMAC.ReportingSystem.Business.Entities.CallingIVR Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procCallingIVRByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public CallingIVRCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procCallingIVRSelect", null);
            return ReadTable(dt) as CallingIVRCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.CallingIVR ReadRow(System.Data.DataRow row)
        {
            CallingIVR CallingIVR = null;
            if (row != null)
            {
                try
                {
                    CallingIVR = new CallingIVR();
                    CallingIVR.DateCall = DateTime.Parse(row["DateCall"].ToString());
                    CallingIVR.Calls = int.Parse(row["Calls"].ToString());
                    CallingIVR.ID = int.Parse(row["ID"].ToString());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return CallingIVR;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.CallingIVR> ReadTable(System.Data.DataTable table)
        {
            try
            {
                CallingIVRCollection CallingIVRs = new CallingIVRCollection();
                CallingIVR CallingIVR;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CallingIVR = ReadRow(row);
                        if (CallingIVR != null)
                        {
                            CallingIVRs.Add(CallingIVR);
                        }
                    }
                }
                return CallingIVRs;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public CallingIVRCollection Search(DateTime? DateCall, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { DateCall, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procCallingIVRByFilterSelect", parameters);
                CallingIVRCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as CallingIVRCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }

    }
}

